package com.zzyl.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.List;
import java.util.Set;

@SpringBootTest
public class RedisTes {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @Test
    public void test() {
        System.out.println(redisTemplate);
    }

    @Test
    public void test1(){
        redisTemplate.opsForHash().put("user", "name", "zhangsan");
        redisTemplate.opsForHash().put("user", "age", "18");
        redisTemplate.opsForHash().put("user", "sex", "man");
        System.out.println(redisTemplate.opsForHash().get("user", "name"));

        Set<Object> user = redisTemplate.opsForHash().keys("user");
        System.out.println(user);

        List<Object> user1 = redisTemplate.opsForHash().values("user");
        System.out.println(user1);
    }

    @Test
    public void test2(){
        redisTemplate.opsForList().leftPushAll("hmy", "1", "2", "3", "4", "5");
        redisTemplate.opsForList().leftPush("hmy", "6");
        System.out.println(redisTemplate.opsForList().range("hmy", 0, -1));
        System.out.println(redisTemplate.opsForList().leftPop("hmy"));
        System.out.println(redisTemplate.opsForList().size("hmy"));
    }
    @Test
    public void test3(){
       redisTemplate.opsForSet().add("myset1",  "1", "2", "3", "4", "5");
       redisTemplate.opsForSet().add("myset2", "1", "2", "3", "4", "5", "6");
        Set<Object> myset1 = redisTemplate.opsForSet().members("myset1");
        System.out.println(myset1);

        redisTemplate.opsForSet().size("myset1");
        System.out.println(redisTemplate.opsForSet().size("myset1"));

        Set<Object> intersect = redisTemplate.opsForSet().intersect("myset1", "myset2");
        System.out.println(intersect);


    }

    @Test
    public void test4(){
        redisTemplate.opsForZSet().add("zset1","a",  1);
        redisTemplate.opsForZSet().add("zset1","b",  2);
        redisTemplate.opsForZSet().add("zset1","c",  3);
        redisTemplate.opsForZSet().add("zset1","d",  4);

        Set<Object> zset1 = redisTemplate.opsForZSet().range("zset1", 0, -1);
        System.out.println(zset1);

        //ZSetOperations.TypedTuple<Object>的作用：因为此时取得是value和score两个值，因此用TypedTuple<Object>封装起来
        Set<ZSetOperations.TypedTuple<Object>> zset11 = redisTemplate.opsForZSet().rangeWithScores("zset1", 0, -1);
        for (ZSetOperations.TypedTuple<Object> typedTuple : zset11){
            System.out.println(typedTuple.getValue() + ":" + typedTuple.getScore());
        }

        redisTemplate.opsForZSet().remove("zset1", "a");
    }

    @Test
    public void test5(){
        System.out.println(redisTemplate.keys("*"));

        Boolean zset1 = redisTemplate.hasKey("zset1");
        System.out.println(zset1);

        DataType myset1 = redisTemplate.type("myset1");
        System.out.println(myset1);

        redisTemplate.delete("myset1");
    }

    @Test
    public void test6(){

    }

}
